using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace WPCoco.Tasks
{
    public class Vector2Animation : TargetedTask<object>
    {
        public Vector2Animation(string propertyName, Vector2 start, Vector2 end, float duration)
            : base(duration)
        {
            this.propertyName = propertyName;
            this.start = start; this.end = end;
        }

        PropertyInfo pinfo;
        string propertyName;
        Vector2 start, end, delta;

        public override void InitializeWithTarget(object target)
        {
            if (target == null) return;
            Type t = target.GetType();
            pinfo = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            delta = ((end - start) / Duration);
        }

        public override void UpdateWithTarget(object target, float time)
        {
            if (pinfo == null) return;
            Vector2 value = (start + delta * time);
            pinfo.SetValue(target, value, null);
        }

        public override void FinishWithTarget(object target)
        {
            if (pinfo == null) return;
            pinfo.SetValue(target, end, null);
        }

        public override Task Clone()
        {
            return new Vector2Animation(propertyName, start, end, Duration);
        }

        public override Task Reverse()
        {
            return new Vector2Animation(propertyName, end, start, Duration);
        }
    }
}
